package org.example.controller;

import org.example.controller.viewobject.ItemVO;
import org.example.error.BusinessException;
import org.example.response.CommonReturnType;
import org.example.service.ItemService;
import org.example.service.RecommendationService;
import org.example.service.model.ItemModel;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author itmei
 * @Package org.example.controller
 * @Date 2025/6/1 15:13
 */
@Controller("item")
@RequestMapping("/item")
public class ItemController extends BaseController{
    @Autowired
    private ItemService itemService;

    @Autowired
    private RecommendationService recommendationService;


    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public CommonReturnType createItem(@RequestParam(name = "title")String title,
                                       @RequestParam(name="description")String description,
                                       @RequestParam(name = "price") BigDecimal price,
                                       @RequestParam(name = "imgUrl")String imgUrl,
                                       @RequestParam(name = "stock")Integer stock) throws BusinessException {
        //封装service请求用来创建商品
        ItemModel itemModel=new ItemModel();
        itemModel.setTitle(title);
        itemModel.setPrice(price);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);
        itemModel.setDescription(description);

        ItemModel itemModelForReturn=itemService.createItem(itemModel);

        ItemVO itemVO=convertVoFromModel(itemModelForReturn);
        return CommonReturnType.create(itemVO);
    }

    //商品详情页浏览

    @RequestMapping(value = "/get",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getItem(@RequestParam(name = "id")Integer id){
        ItemModel itemModel=itemService.getItemById(id);
        ItemVO itemVo=convertVoFromModel(itemModel);
        return CommonReturnType.create(itemVo);
    }
    //商品列表页面浏览
    @RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonReturnType listItem(){
        List<ItemModel> itemModelList=itemService.listItem();
        //使用stram api将itemModellist内的itemModel转为ItemVo
        List<ItemVO> itemVOList=itemModelList.stream().map(itemModel -> {
            ItemVO itemVO=this.convertVoFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
      return CommonReturnType.create(itemVOList);
    }

    //获取热门商品列表
    @RequestMapping(value = "/hotlist",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonReturnType listHotItems(){
        List<ItemModel> itemModelList=itemService.listHotItems(10);
        List<ItemVO> itemVOList=itemModelList.stream().map(itemModel -> {
            ItemVO itemVO=this.convertVoFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
        return CommonReturnType.create(itemVOList);
    }

    //分页查询商品列表
    @RequestMapping(value = "/listpage",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonReturnType listPage(@RequestParam(name = "page",defaultValue = "1")int page,
                                     @RequestParam(name = "limit",defaultValue = "10")int limit){
        CommonReturnType commonReturnType=new CommonReturnType();
        List<ItemModel> itemModelList=itemService.listItemByPage(page,limit);
        List<ItemVO> itemVOList=itemModelList.stream().map(itemModel -> {
            ItemVO itemVO=this.convertVoFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
        commonReturnType.setData(itemVOList);
        return commonReturnType;
    }

    /**
     * 基于用户协同过滤推荐商品列表
     * @param userId
     * @param limit
     * @return
     */
    @RequestMapping(value = "/recommend",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonReturnType lsitRecommendItemsByUserCF(@RequestParam(name = "userId")Integer userId,
                                                       @RequestParam(name = "limit",defaultValue = "50")int limit){
        List<ItemModel> itemModelList= recommendationService.getRecommendItemsByUserCF(userId,limit);
        List<ItemVO> itemVOList=itemModelList.stream().map(itemModel -> {
            ItemVO itemVO=this.convertVoFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
        return CommonReturnType.create(itemVOList);
    }

    /**
     * 将itemModel转换为itemVO
     * @param itemModel
     * @return
     */
    private ItemVO convertVoFromModel(ItemModel itemModel){
        if(itemModel==null){
            return null;
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemModel,itemVO);
        if(itemModel.getPromoModel()!=null){
            //有正在进行或即将进行的秒杀活动
            itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVO.setPromoId(itemModel.getPromoModel().getId());
            itemVO.setPromoStartDate(itemModel.getPromoModel().getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
            itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
        }else {
            itemVO.setPromoStatus(0);
        }
        return itemVO;
    }

    @RequestMapping(value = "/searchItems",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonReturnType searchItems(@RequestParam(name = "keyword")String keyword,
                                        @RequestParam(name = "page",defaultValue = "1")int page,
                                        @RequestParam(name = "limit",defaultValue = "10")int limit){
        List<ItemModel> itemModelList=itemService.searchItems(keyword,page,limit);
        List<ItemVO> itemVOList=itemModelList.stream().map(itemModel -> {
            ItemVO itemVO=this.convertVoFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
        return CommonReturnType.create(itemVOList);
    }

}
